package com.seres.data;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class Main {
	public static void main(String[] args){
		Main obj = new Main();
		

	}
	
	public CoreModule saveCoreModule(Scenario firstScenario){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		CoreModule module = null;
		try {
			transaction = session.beginTransaction();
		
			module = new CoreModule();
			module.setFirstScenario(firstScenario);
			module.setModuleType(ModuleType.CORE);
			
			session.save(module);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return module;
	}
	public GameStructure saveGameStructure(String structureName,List<CoreModule> modules){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		GameStructure structure = null;
		try {
			transaction = session.beginTransaction();
		
			structure = new GameStructure();
			structure.setStructureName(structureName);
			structure.setCoreModules(modules);
			
			session.save(structure);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return structure;
	}
	public Scenario saveScenario(String shortDescription,List<String> descriptions,List<Answer> possibleAnswers,String imageName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Scenario scenario = null;
		try {
			transaction = session.beginTransaction();
		
			scenario = new Scenario();
//			scenario.setDescriptions(descriptions);
			scenario.setShortDescription(shortDescription);
			scenario.setPossibleAnswers(possibleAnswers);
//			scenario.setImageName(imageName);
		
			
			session.save(scenario);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return scenario;
	}
	public Answer saveAnswer(String shortDescription,String fullDescription,List<IndexImpact> indexImpacts,
			List<StakeholderImpact> stakeholderImpacts){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Answer answer = null;
		try {
			transaction = session.beginTransaction();
		
			answer = new Answer();
			answer.setFullDescription(fullDescription);
			answer.setShortDescription(shortDescription);
			answer.setStakeholderImpacts(stakeholderImpacts);
			answer.setIndexImpacts(indexImpacts);
		
			session.save(answer);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return answer;
	}
	public void saveAceCategory(String categoryName,List<Ace> aces){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
		
			AceCategory aceCategory = new AceCategory();
		
			aceCategory.setCategoryName(categoryName);
			aceCategory.setAces(aces);
			
			session.save(aceCategory);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}
	public void saveAceCategory(String categoryName){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
		
			AceCategory aceCategory = new AceCategory();
		
			aceCategory.setCategoryName(categoryName);
		
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}
	public void listIndexImpact(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<IndexImpact> impacts = session.createQuery("from IndexImpact").list();
			for (Iterator iterator = impacts.iterator(); iterator.hasNext();)
			{
				IndexImpact impact = (IndexImpact) iterator.next();
				System.out.println(impact.getImpactId() + ": " + impact.getIndex().toString());
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	
	public void listAces(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<Ace> aces = session.createQuery("from Ace").list();
			for (Iterator iterator = aces.iterator(); iterator.hasNext();)
			{
				Ace ace = (Ace) iterator.next();
				System.out.println(ace.getAceId() + ": " + ace.getFullDescription());
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void listAcesUsingAceCategory(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<AceCategory> aceCategories = session.createQuery("from AceCategory").list();
			for (Iterator iterator = aceCategories.iterator(); iterator.hasNext();)
			{
				AceCategory aceCategory = (AceCategory) iterator.next();
				List<Ace> aces = aceCategory.getAces();
				for (Iterator iterator2 = aces.iterator(); iterator2.hasNext();){
					Ace ace = (Ace) iterator2.next();
					System.out.println(ace.getAceId() + ": " + ace.getFullDescription());
				}

			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public void listAceCategory(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<AceCategory> list = session.createQuery("from AceCategory").list();
			for (Iterator iterator = list.iterator(); iterator.hasNext();)
			{
				AceCategory aceCategory = (AceCategory) iterator.next();
				System.out.println(aceCategory.getCategoryName());
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	public IndexImpact saveIndexImpact(float value, Index index){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int impactId = 0;
		IndexImpact indexImpact = null;
		try {
		
			transaction = session.beginTransaction();
		
			indexImpact = new IndexImpact();
			indexImpact.setIndex(index);
			indexImpact.setValue(value);
		
			impactId = (Integer) session.save(indexImpact);
		
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexImpact;
	}
	public StakeholderImpact saveStakeholderImpact(float value, Stakeholder stakeholder){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int impactId = 0;
		StakeholderImpact stakeholderImpact = null;
		try {
		
			transaction = session.beginTransaction();
		
			stakeholderImpact = new StakeholderImpact();
			stakeholderImpact.setStakeholder(stakeholder);
			stakeholderImpact.setValue(value);
		
			impactId = (Integer) session.save(stakeholderImpact);
		
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return stakeholderImpact;
	}
	public Ace saveAce(String shortDesc, String fullDesc, List<IndexImpact> indexImpacts){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		int aceId = 0;
		Ace ace = null;
		try {
		
			transaction = session.beginTransaction();
		
			ace = new Ace();
		
			ace.setShortDescription(shortDesc);
			ace.setFullDescription(fullDesc);
			ace.setIndexImpacts(indexImpacts);
			
			aceId = (Integer) session.save(ace);
		
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return ace;
	}
//	private void test(){
//		Session session = HibernateUtil.getSessionFactory().openSession();
//		Transaction transaction = null;
//		try {
//		transaction = session.beginTransaction();
		
//		Ace ace = new Ace();
//		ace.setId(1);
//		ace.setShortDescription("js");
//		ace.setFullDescription("jos smos");
//		
//		session.save(ace);
//		transaction.commit();		
//		
//		AceCategory cat = new AceCategory();
//		cat.setCategoryName("cat name");
//		
//		List<Ace> aces = new ArrayList<Ace>();
//		aces.add(ace);
//		cat.setAces(aces);
//		session.save(cat);
//		
//		transaction.commit();
		
//		List tests = session.createQuery("from AceCategory").list();
//		for (Iterator iterator = tests.iterator(); iterator.hasNext();)
//		{
//			AceCategory testje = (AceCategory) iterator.next();
//			System.out.println(testje.getCategoryName());
//		}
//		List test = session.createQuery("from Ace").list();
//		for (Iterator iterator = test.iterator(); iterator.hasNext();)
//		{
//			Ace testjee = (Ace) iterator.next();
//			System.out.println(testjee.getFullDescription());
//		}
//		transaction.commit();
//		
//		} catch (HibernateException e) {
//			transaction.rollback();
//			e.printStackTrace();
//		} finally {
//			session.close();
//		}
//	}
}
